package com.anxin.registerManagement.service;

import cn.hutool.core.util.ObjectUtil;
import com.anxin.registerManagement.bean.dto.DormitoryDto;
import com.anxin.registerManagement.bean.jo.DormitorySearchJo;
import com.anxin.registerManagement.bean.persist.Dormitory;
import com.anxin.registerManagement.mapper.DormitoryMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 叶前呈
 * @date 2022/1/29 16:06
 */
@Service
public class DormitoryService {

    private static Logger logger = LoggerFactory.getLogger(DormitoryService.class);

    @Autowired
    DormitoryMapper dormitoryMapper;

    /**
     * 查询宿舍
     */
    public PageInfo<DormitoryDto> getDormitoryList(DormitorySearchJo dormitorySearchJo) {
        if (!ObjectUtil.isEmpty(dormitorySearchJo.getPageNum()) && !ObjectUtil.isEmpty(dormitorySearchJo.getPageSize())) {
            PageHelper.startPage(dormitorySearchJo.getPageNum(), dormitorySearchJo.getPageSize());
        }
        List<DormitoryDto> dormitoryDtoList = dormitoryMapper.getDormitoryList(dormitorySearchJo);
        return new PageInfo<>(dormitoryDtoList);
    }


    /**
     * 添加宿舍
     */
    public void addDormitory(Dormitory dormitory) {
        // TODO:判断楼幢和楼长id是否存在即可
        dormitoryMapper.insert(dormitory);
        logger.info("添加宿舍成功");
    }

    /**
     * 修改宿舍
     */
    public void updateDormitory(Dormitory dormitory) {
        // TODO:判断楼幢和楼长id是否存在即可
        dormitoryMapper.updateById(dormitory);
        logger.info("修改宿舍成功");
    }

    /**
     * 删除宿舍
     */
    public void deleteDormitory(int id) {
        // TODO:这里需要判断，宿舍与房间是否有关联，如果有关联不能删除宿舍
        dormitoryMapper.deleteById(id);
        logger.info("删除宿舍id：{}，成功", id);
    }

    /**
     * 获取所有宿舍
     */
    public List<Dormitory> getAllDormitory() {
        return dormitoryMapper.selectList(null);
    }
}
